#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import opengate as gate
import opengate.contrib.spect.ge_discovery_nm670 as spect_ge_nm670
from pathlib import Path

if __name__ == "__main__":
    # create the simulation
    sim = gate.Simulation()

    # main options
    sim.g4_verbose = False
    sim.visu = False
    sim.visu_type = "vrml"
    # sim.visu_type = "qt"
    sim.number_of_threads = 6
    sim.random_seed = "auto"
    sim.progress_bar = True
    sim.output_dir = "./output"

    # units
    m = gate.g4_units.m
    sec = gate.g4_units.second
    days = 3600 * 24 * sec
    cm = gate.g4_units.cm
    mm = gate.g4_units.mm
    nm = gate.g4_units.nm
    MeV = gate.g4_units.MeV
    keV = gate.g4_units.keV
    Bq = gate.g4_units.Bq
    kBq = 1000 * Bq
    MBq = 1000 * kBq

    # world size
    sim.world.size = [2 * m, 2 * m, 2 * m]
    sim.world.material = "G4_AIR"

    # waterbox
    wb = sim.add_volume("Box", "waterbox")
    wb.size = [30 * cm, 30 * cm, 30 * cm]
    wb.material = "G4_AIR"
    wb.color = [0, 0, 1, 1]  # blue

    # spect head (debug mode = only a small part of the collimator is simulation,
    # for visu mode)
    # - False : no collimator
    # - lehr : holes length 35 mm, diam 1.5 mm, septal thickness : 0.2 mm
    # - megp : holes length 58 mm, diam 3 mm,   septal thickness : 1.05 mm
    # - hegp : holes length 66 mm, diam 4 mm,   septal thickness : 1.8 mm
    spect, colli, crystal = spect_ge_nm670.add_spect_head(
        sim, "spect", collimator_type="megp", debug=(sim.visu and sim.visu_type != "qt")
    )

    spect.translation = [0, 0, -28 * cm]

    # spect digitizer
    channels = [
        {"name": f"spectrum", "min": 3 * keV, "max": 515 * keV},
        {"name": f"scatter", "min": 380 * keV, "max": 420 * keV},
        {"name": f"peak", "min": 400 * keV, "max": 450 * keV},
    ]
    # spect digitizer
    proj = spect_ge_nm670.add_digitizer(sim, "spect_crystal", channels)
    # proj is the Projection image
    proj.output_filename = Path("output") / "projection2_star.mhd"

    # Lu177 source (only the gammas)
    source = sim.add_source("GenericSource", "fake")
    source.particle = "gamma"
    source.attached_to = f"waterbox"

    source.energy.type = "spectrum_discrete"
    source.energy.spectrum_weights = [0.114, 0.259]
    source.energy.spectrum_energies = [218.12 * keV, 440.45 * keV]
    source.position.type = "sphere"
    source.position.radius = 1 * mm
    source.position.translation = [0, 0, 0 * mm]
    source.direction.type = "iso"
    if sim.visu:
        sim.number_of_threads = 1
        source.activity = 2 * Bq
    else:
        source.activity = 4 * MBq / sim.number_of_threads
    source.half_life = 6.647 * days

    # add stat actor
    stats = sim.add_actor("SimulationStatisticsActor", "stats")
    stats.track_types_flag = True
    stats.output_filename = "stats2_star.txt"

    # phys
    sim.physics_manager.physics_list_name = "G4EmStandardPhysics_option3"
    sim.physics_manager.set_production_cut("world", "all", 1 * mm)

    # ---------------------------------------------------------------------
    # start simulation
    # sim.running_verbose_level = gate.EVENT
    sim.run_timing_intervals = [[0, 10 * sec]]
    sim.run()

    # print stats
    print(stats)
